package development;

import state.GameAlert;
import state.GameControl;
import state.RecordStore;
import state.md5.MD5;
import state.socket.DataPackage;
import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.array.ByteArray;
import state.util.array.StringArray;
import development.bean.LoginInfo;
import development.bean.Provider;

public class Rms {
  public final static String LOGIN_RMS = "ccc";
  public final static String PROVIDER_RMS = "ddd"; // Lưu providerId
  public final static String SESSION_RMS = "eee"; // Lưu thông tin session
  public final static String AVATAR_CACHE_RMS = "fff"; // Lưu thông tin cache avatar
  public final static String CONTACT_LIST_RMS = "ggg"; // Lưu thông tin danh sách contact của người dùng để gửi về server để SPAM
  
  public static String TABLE_6_PLAYER_IMAGE_CATEGORY = "table6";
  public static String MANDATORY_CATEGORY = "mandatory";
  public static String DAO_VANG_IMAGE_CATEGORY = "daovang";
  public static String AUDITION_IMAGE_CATEGORY = "audition";
  public static String AUDITION_320x240_IMAGE_CATEGORY = "audition_320x240";
  public static String AUDITION_EXTENSION_CATEGORY = "audition_extension";
  public static String AUDITION_LAZY_CATEGORY = "audition_lazy";
  public static String CO_TUONG_IMAGE_CATEGORY = "cotuong";
  public static String CO_VUA_IMAGE_CATEGORY = "covua";
  public static String BA_CAY_IMAGE_CATEGORY = "bacay";
  public static String XI_TO_IMAGE_CATEGORY = "xito";
  public static String AI_LA_TRIEU_PHU_IMAGE_CATEGORY = "ailatrieuphu";
  public static String DUOI_HINH_BAT_CHU_IMAGE_CATEGORY = "duoihinhbatchu";
  public static String CAM_NANG_CATEGORY = "18+";
  public static String CUA_HANG_CATEGORY = "cuahang";
  public static String KET_BAN_CATEGORY = "ketban";
  public static String SHOP_CATEGORY = "shop";
  public static String THOI_TIET_CATEGORY = "thoitiet";
  public static String XO_SO_CATEGORY = "xoso";
  public static String LTH_CATEGORY = "lichtruyenhinh";
  public static String MANGXH_CATEGORY = "mangxh";
  public static String CHAT_CATEGORY = "chat";
  
  public static void clearAllRms() {
    Global.alert.showAlert(null, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang xóa dữ liệu...");
    new GameControl() {
      public void perform() {
        StringArray rmsToClear = RecordStore.listRecordStores(); 
        for (int i = 0; i < rmsToClear.len(); i++) {
          RecordStore.deleteRecordStore(rmsToClear.array[i]);
        }
        Global.resourceCategory = new Hashtable();
        Global.alert.showAlert(null, GameAlert.OK_TYPE, "Xóa dữ liệu thành công!");
      }
    }.setTimeInterval(1000).start();
  }

  public static Provider loadProvider() {
    Provider provider = new Provider(Constants.DEFAULT_PROVIDER_ID);
    RecordStore recordStore = RecordStore.openRecordStore(PROVIDER_RMS);
    if (recordStore.getNumRecords() > 0) {
      DataPackage dataPackage = DataPackage.createDataPackage(recordStore.getRecord(1));
      provider.setId(dataPackage.nextInt());
      provider.setAccountName(dataPackage.nextString());
    } else {
      saveProvider(provider);
    }
    return provider;
  }

  public static void saveProvider(Provider provider) {
    RecordStore.deleteRecordStore(PROVIDER_RMS);
    RecordStore recordStore = RecordStore.openRecordStore(PROVIDER_RMS);
    DataPackage dataPackage = DataPackage.createDataPackage1(0);
    dataPackage.putInt(provider.getId());
    dataPackage.putString(provider.getAccountName());
    ByteArray data = dataPackage.getAllData();
    recordStore.addRecord(data, 0, data.len());
  }
  
  public static String getSession(String username) {
     RecordStore recordStore = RecordStore.openRecordStore(SESSION_RMS);
    if (recordStore.getNumRecords() > 0) {
      DataPackage dataPackage = DataPackage.createDataPackage(recordStore.getRecord(1));
      String storedUsername = dataPackage.nextString();
      String sessionId = dataPackage.nextString();
      if (storedUsername.equals(username)) {
        return sessionId;
      }
    }
    return null;
  }
  
  public static void saveSession(String username, String sessionId) {
    RecordStore.deleteRecordStore(SESSION_RMS);
    RecordStore recordStore = RecordStore.openRecordStore(SESSION_RMS);
    DataPackage dataPackage = DataPackage.createDataPackage1(0);
    dataPackage.putString(username);
    dataPackage.putString(sessionId);
    ByteArray data = dataPackage.getAllData();
    recordStore.addRecord(data, 0, data.len());
  }

  public static void deleteSession() {
    RecordStore.deleteRecordStore(SESSION_RMS);
  }

  public static LoginInfo loadLoginInfo() {
    LoginInfo loginInfo = new LoginInfo();
     RecordStore masterRecordStore = RecordStore.openRecordStore(LOGIN_RMS);
    if (masterRecordStore.getNumRecords() > 0) {
      loginInfo.setHasData(true);
      loginInfo.setSaveUserNameAndPassword(masterRecordStore.getRecord(1).get(0) == 1 ? true : false);
      loginInfo.setAutoLogin(masterRecordStore.getRecord(2).get(0) == 1 ? true : false);
      loginInfo.setUuid(new String(masterRecordStore.getRecord(3).array)); // TODO: có thể new String(byte[]) ko đc phép dùng ở đây
      if ((loginInfo.isSaveUserNameAndPassword() || loginInfo.isAutoLogin()) && masterRecordStore.getNumRecords() > 3) {
        loginInfo.setUserName(new String(masterRecordStore.getRecord(4).array));
        loginInfo.setPassword(new String(masterRecordStore.getRecord(5).array));
        Global.currentUser.setName(loginInfo.getUserName());
        Global.currentUser.setPasswordMd5(MD5.toBase64(GameStringUtils.toByteArrayUTF8(loginInfo.getPassword())));
      }
    }
    return loginInfo;
  }

  public static void saveLoginInfo(LoginInfo loginInfo) {
    RecordStore.deleteRecordStore(LOGIN_RMS);
    RecordStore masterRecordStore = RecordStore.openRecordStore(LOGIN_RMS);
    masterRecordStore.addRecord(loginInfo.isSaveUserNameAndPassword() ? new ByteArray(new byte[] { 1 }, 1) : new ByteArray(new byte[] { 0 }, 1), 0, 1);
    masterRecordStore.addRecord(loginInfo.isAutoLogin() ? new ByteArray(new byte[] { 1 }, 1) : new ByteArray(new byte[] { 0 }, 1), 0, 1);
    ByteArray uuid = GameStringUtils.toByteArrayUTF8(loginInfo.getUuid());
    masterRecordStore.addRecord(uuid, 0, uuid.len());
    if (loginInfo.isSaveUserNameAndPassword() || loginInfo.isAutoLogin()) {
      ByteArray userNameBytes = GameStringUtils.toByteArrayUTF8(loginInfo.getUserName());
      masterRecordStore.addRecord(userNameBytes, 0, userNameBytes.len());

      ByteArray passwordBytes = GameStringUtils.toByteArrayUTF8(loginInfo.getPassword());
      masterRecordStore.addRecord(passwordBytes, 0, passwordBytes.len());
    }
  }
}
